{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4b0f9b48",
   "metadata": {},
   "source": [
    "--------   \n",
    "* time：2021-11-29  \n",
    "* week14  \n",
    "--------  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1616ea7b",
   "metadata": {},
   "source": [
    "# 本周内容  \n",
    ">  1.函数的定义  \n",
    ">  2.如何使用函数   \n",
    ">  3.函数的返回值  \n",
    ">  4.使用注释改进文档  \n",
    ">  5.传递任意数量的实参  \n",
    ">  6.函数与模块   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4e1c618f",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-1-34786c5e2d22>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-1-34786c5e2d22>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    def:定义函数的关键字\u001b[0m\n\u001b[1;37m       ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "def:定义函数的关键字\n",
    "fib：函数名\n",
    "(n)：定义函数的参数\n",
    "\"\"\"\"\"\"：文档描述\n",
    "代码块：\n",
    "     a, b = 0, 1\n",
    "     while a < n:\n",
    "        print(a, end=' ')\n",
    "         a, b = b, a+b\n",
    "     print()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f081dbee",
   "metadata": {},
   "source": [
    "## 1.函数的定义  \n",
    ">  1. 写def：def定义函数的第一步  \n",
    ">  2. 写定义函数的名称：def function_name \n",
    ">>  1.function_name遵循变量名称定义的规则 \n",
    ">  3.函数名称后加上\"():\"：def function_name():===>  f(x) = 2x  \n",
    "\n",
    "* 书写函数体  \n",
    ">  1.函数的注释  \n",
    ">  2.写函数的具体操作（函数体） \n",
    ">  3.注意：函数也是需要有tab键的间距缩进   \n",
    "\n",
    "* 使用函数 （函数的调用）  \n",
    ">  1.function_name()\n",
    "* 函数的意义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "ff7ed989",
   "metadata": {},
   "outputs": [],
   "source": [
    "def fib(n):    # write Fibonacci series up to n\n",
    "    \"\"\"Print a Fibonacci series up to n.\"\"\"\n",
    "    a, b = 0, 1\n",
    "    while a < n:\n",
    "         print(a, end=' ')\n",
    "         a, b = b, a+b\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "5b285dfd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 1 2 3 5 8 \n"
     ]
    }
   ],
   "source": [
    "fib(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "f3bf58dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 尝试：写一个实现 hello world的函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "b0aafa78",
   "metadata": {},
   "outputs": [],
   "source": [
    "def hello():\n",
    "    \"\"\"打印hello world\"\"\"\n",
    "    print(\"hello world\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "62a7cc66",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n"
     ]
    }
   ],
   "source": [
    "hello()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "90a06453",
   "metadata": {},
   "source": [
    "## 如何使用函数（课本实践P149）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b2e118b5",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 思考函数存在的意义？  \n",
    "# 1. 可以通过调用函数的方式实现某个功能，使用人不需要考虑函数代码内容\n",
    "# 2. 节省时间，提升应用的效率\n",
    "# 3. 函数一旦定义，就可按照一定的规则重复使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "71888f21",
   "metadata": {},
   "outputs": [],
   "source": [
    "#vowels = set('aeiou')\n",
    "#word = input(\"Provide a word to search for vowels:\")\n",
    "#found = vowels.intersection(set(word))\n",
    "#found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "46ac60dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "#  定义了一个名字叫做“search4vowels”的函数\n",
    "def search4vowels(word):\n",
    "    \"\"\"\n",
    "    打印文本中出现的元音字母\n",
    "    请输入你想要查询的文本内容\n",
    "    \"\"\"\n",
    "    vowels = set('aeiou')\n",
    "    found = vowels.intersection(set(word)) #交集运算\n",
    "    print(found)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "2be3d524",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function search4vowels in module __main__:\n",
      "\n",
      "search4vowels(word)\n",
      "    打印文本中出现的元音字母\n",
      "    请输入你想要查询的文本内容\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(search4vowels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "5f3f6b88",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'i', 'o'}\n"
     ]
    }
   ],
   "source": [
    "search4vowels(\"gkojiobbbti\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "30a42465",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'e', 'i', 'u', 'o', 'a'}\n"
     ]
    }
   ],
   "source": [
    "search4vowels(\"gjioowdwtueuiabx\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "b0b882fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'e', 'i', 'u', 'o', 'a'}\n"
     ]
    }
   ],
   "source": [
    "search4vowels(\"woiokxnahyuwwez\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b9869f6",
   "metadata": {},
   "source": [
    "* 下面的方法是不是太重复了，主要原因是没有对重复功能的代码块有特殊的定义（如：名称，注释等）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "8ba9f06e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:ssiwllox\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'i', 'o'}"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels = set('aeiou')\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = vowels.intersection(set(word))\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "ae172ec5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:jhsoioosn\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'i', 'o'}"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels = set('aeiou')\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = vowels.intersection(set(word))\n",
    "found"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c2637b9f",
   "metadata": {},
   "source": [
    "## 3. 函数的参数  \n",
    "\n",
    "> 1. 形式参数（形参）  \n",
    ">> 1.在定义过程中，函数的参数为形式参数 \n",
    ">> 2.形式参数是虚拟的，不占空间的\n",
    "> 2.实际参数（实参）  \n",
    ">> 1.调用函数过程中，函数的参数为实际函数\n",
    ">> 2.调用函数时，形式参数被分配空间，成为实际参数并应用起来"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "edfa9c6b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义函数\n",
    "def greet_user(username):    \n",
    "    \"\"\"显示简单的问候语，输入你的名字\"\"\"    \n",
    "    print(\"hello,\"+username.title()+\"!\")          "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "42ed1a63",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello,Zhichao!\n"
     ]
    }
   ],
   "source": [
    "# 调用函数    username = “zhichao”\n",
    "greet_user(\"zhichao\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "c41de4e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 请注意，有两个参数\n",
    "def describe_pet(pet_name,animal_type=\"dog\",):\n",
    "    \"\"\"显示宠物信息\"\"\"\n",
    "    print(\"\\nI have a \" + animal_type + \".\")\n",
    "    print(\"My \" + animal_type + \"'s name is \"+pet_name.title() + \".\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "ec97b9e2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a dog.\n",
      "My dog's name is 旺财.\n"
     ]
    }
   ],
   "source": [
    "describe_pet(\"旺财\",\"dog\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "783007ee",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
